package com.whxy02.tolearn.app.utils;

import com.whxy02.tolearn.app.resultObject.ResultState;
import org.springframework.stereotype.Component;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.*;
import java.util.Map;

@Component
public class Upload {

    @Resource
    PackResult packResult;

    public String upload(MultipartFile file, String prefix,String type, Integer id){
        InputStream in;
        OutputStream out;

        try{
            in=file.getInputStream();
            File targetfile=new File("src\\main\\resources\\static\\"+prefix+"\\"+type+"_"+id+file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")));
            System.out.println(targetfile.getPath());
//            file.transferTo(targetfile);          //使用绝对路径
            out=new FileOutputStream(targetfile);
            FileCopyUtils.copy(in,out);
            return "/"+prefix+"/"+type+"_"+id+file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }
}
